package model

import (
	"github.com/asaskevich/govalidator"
)

func init() {
	govalidator.CustomTypeTagMap.Set("memory", MemoryValidate)
	govalidator.CustomTypeTagMap.Set("runtime", RuntimeValidate)
	govalidator.CustomTypeTagMap.Set("runtimes", RuntimesValidate)
	govalidator.CustomTypeTagMap.Set("custom-container-port", CustomContainerPortValidate)
}

func MemoryValidate(i interface{}, o interface{}) bool {

	mem := i.(int)

	if mem < MinFunctionMemory || mem > MaxFunctionMemory || mem%FunctionMemoryInterval != 0 {
		return false
	}
	return true
}

func RuntimeValidate(i interface{}, o interface{}) bool {
	runtime := i.(string)
	if !RuntimeSet.Has(runtime) {
		return false
	}
	return true
}

func RuntimesValidate(i interface{}, o interface{}) bool {
	runtimes := i.([]string)
	for _, runtime := range runtimes {
		if !RuntimeSet.Has(runtime) {
			return false
		}
	}
	return true
}

func CustomContainerPortValidate(i interface{}, o interface{}) bool {
	port := i.(string)
	// 不能使用 knative 占用的几个端口
	if UserContainerPortSet.Has(port) {
		return false
	}
	return true
}
